package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.ljfiction.book.MyView.SuperButton;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.Md5;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.PreferencesTool;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class UpdataPassWordActivity extends BaseActivity {

    @BindView(R.id.updatapasswordback)
    ImageView updatapasswordback;
    @BindView(R.id.oldpossword)
    EditText oldpossword;
    @BindView(R.id.newpassword)
    EditText newpassword;
    @BindView(R.id.updatapasswordok)
    SuperButton updatapasswordok;
    @BindView(R.id.updata_firsteyes)
    ImageView updataFirsteyes;
    @BindView(R.id.updata_toweyes)
    ImageView updataToweyes;
    @BindView(R.id.secoundpassword)
    EditText secoundpassword;
    @BindView(R.id.updata_threeeyes)
    ImageView updataThreeeyes;
private boolean firsteye=false,secoundeye=false,thirdeye=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updata_pass_word);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.updatapasswordback, R.id.updatapasswordok,R.id.updata_firsteyes,R.id.updata_toweyes,R.id.updata_threeeyes})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.updatapasswordback:
                goBack();
                break;
            case R.id.updatapasswordok:
                if (StringTool.isEmpty(oldpossword.getText().toString())) {
                    showToast("请输入旧密码");
                    return;
                }else if (!StringTool.isPass(oldpossword.getText().toString())) {
                    showToast(StringTool.isPassString(oldpossword.getText().toString()));
                    return;
                  } else if (StringTool.isEmpty(newpassword.getText().toString())) {
                    showToast("请输入新密码");
                    return;
                }else if (!StringTool.isPass(newpassword.getText().toString())) {
                    showToast(StringTool.isPassString(newpassword.getText().toString()));
                    return;
                } else if (StringTool.isEmpty(secoundpassword.getText().toString())){
                    showToast("请确认新密码");
                    return;
                }else if (!newpassword.getText().toString().equals(secoundpassword.getText().toString())){
                    showToast("两次密码不一致");
                    return;
                }
                updataPossword();
                break;
            case R.id.updata_firsteyes:
                if (firsteye) {
                    firsteye = false;
                    oldpossword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    updataFirsteyes.setImageResource(R.mipmap.eyebihui);
                } else {
                    firsteye = true;
                    oldpossword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    updataFirsteyes.setImageResource(R.mipmap.eyekaihui);
                }
                break;
            case R.id.updata_toweyes:
                if (secoundeye) {
                    secoundeye = false;
                    newpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    updataToweyes.setImageResource(R.mipmap.eyebihui);
                } else {
                    secoundeye = true;
                    newpassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    updataToweyes.setImageResource(R.mipmap.eyekaihui);
                }
                break;
            case R.id.updata_threeeyes:
                if (thirdeye) {
                    thirdeye = false;
                    secoundpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    updataThreeeyes.setImageResource(R.mipmap.eyebihui);
                } else {
                    thirdeye = true;
                    secoundpassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    updataThreeeyes.setImageResource(R.mipmap.eyekaihui);
                }
                break;
        }
    }

    private void updataPossword() {
        OkHttpUtils.post(HttpManager.IP + "/person-updatepwd")

                .params("newPwd", Md5.encryption(newpassword.getText().toString()))
                .params("oldPwd", Md5.encryption(oldpossword.getText().toString()))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject js = new JSONObject(s);
                            if (js.getInt("returnCode") == 200) {
                                PreferencesTool.saveLoginInfo(UpdataPassWordActivity.this, UserManageBean.userBean.getUserName(), newpassword.getText().toString());
                                showToast("修改成功");
                                goBack();
                            } else {
                                if (js.getInt("returnCode") == 400){
                                    Intent intent=new Intent(UpdataPassWordActivity.this,LoginActivity.class);
                                    startActivity(intent);
                                }
                                showToast(js.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }
}
